package ControlLayer;
import java.util.ArrayList;

import ModelLayer.*;
//model layer package has to be imported

/**
 * Controller class for the address book
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AdressBookCtr
{
    private static AdressBookCtr _instance = null;
    private AdressBook adressBook;
    
   
    //declare instance variables to reference containers
    //passed as arguments from the main class
    
    //Address container is to be passed as parameter from the main controller
     public static AdressBookCtr getInstance() 
    {
      if(null == _instance) 
      {
         _instance = new AdressBookCtr();
      }
      return _instance;
   }
   
   private AdressBookCtr()
    {
        adressBook = AdressBook.getInstance();        
    } 
    //Dummy implementation: after implementing the model layer 
    //objects of class Person are to be returned, not String
   public Person getPerson(int id)
    {
        Person person = adressBook.getPerson(id);     
        int index = 0;     
        boolean found = false; 
      
       if (person != null)
       return person;
       else
       return null;
       // while(index < adressBook.getSize() && !found)
        //{
        //    person = adressBook.getPersons().get(index); 
        //    if(person.getId()==(id))
         //   {
         //       found = true;
         //   }
          //  else{
           //      index++;  
            //    }
        // } 
        //if (found){
        //    adressBook.get(index).printPerson();
        //}
        //else
        //{
        //      System.out.println("There doesnt exits any persons with the Id" + id + " in the adressbook");
        //}       
    }
    
    
    public void addPerson(int id, String name, String address, String postalCode, String city, String phoneNumber)
    {
        Person person = new Person(id, name, address, postalCode, city, phoneNumber);
        adressBook.addPerson(person);
    }
    public void addPerson(Person person)
    {
        adressBook.addPerson(person);
    }
    public Person findPerson(String name)
    {
        Person person = adressBook.findPerson(name);     
        int index = 0;     
        boolean found = false; 
      
       if (person != null)
       return person;
       else
       return null;   
    }
    public void removePerson(String name)
    {
        adressBook.removePerson(name);          
    }
    
    public void updatePersonName(String name, String newName)
    {
        Person person = adressBook.findPerson(name);     
        int index = 0;     
        boolean found = false; 
        
       if (person != null)
       {
           person.setName(newName);
           removePerson(name);
           addPerson(person);
        }
        else
        {
             System.out.println("Cant find person");
        }
    }
    
      public void printWholeAddressBook()
      {
          adressBook.printWholeAddressBook();
      }
    
    public void updateAddress(String name, String newAddress)
    {
       Person person = adressBook.findPerson(name);     
        int index = 0;     
        boolean found = false; 
        
       if (person != null)
       {
           person.setAddress(newAddress);
           adressBook.removePerson(person);
           adressBook.addPerson(person);
       }
       else
       {
           System.out.println("Cant find person");
       }
    }
    public void updatePostalCode(String name, String newPostalCode)
    {
        Person person = adressBook.findPerson(name);     
        int index = 0;     
        boolean found = false; 
        
       if (person != null)
       {
           person.setPostalCode(newPostalCode);
           adressBook.removePerson(person); 
           adressBook.addPerson(person);
       }
           else
       {
           System.out.println("Cant find person");
       }
    }
    public void updateCity(String name, String newCity)
    {
        Person person = adressBook.findPerson(name); 
        int index = 0;
        boolean found = false;
        
        if (person != null)
       {
           person.setCity(newCity);
           adressBook.removePerson(person);
           adressBook.addPerson(person);
        }
        else
        {
             System.out.println("Cant find person");
        }
    }
    public void updatePhoneNumber(String name, String newPhoneNumber)
    {
        Person person = adressBook.findPerson(name); 
        int index = 0;
        boolean found = false;
        
          if (person != null)
       {
           person.setCity(newPhoneNumber);
           adressBook.removePerson(person);
           adressBook.addPerson(person);
        }
        else
        {
             System.out.println("Cant find person");
        }
    }
    public void printPerson(String name)
    {
        Person person = adressBook.findPerson(name); 
        int index = 0;
        boolean found = false;
        
         if (person != null)
       {
            person.printPerson();    
       }
        else
        {
             System.out.println("Cant find person");
        }
    }
}
    
    
